package com.mc.servlet;

import com.mc.bean.Jury;
import com.mc.bean.Student;
import com.mc.bean.Teacher;
import com.mc.service.JuryService;
import com.mc.service.StudentService;
import com.mc.service.TeacherService;
import com.mc.service.impl.JuryServiceImpl;
import com.mc.service.impl.StudentServiceImpl;
import com.mc.service.impl.TeacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description Ajax判断电话是否重复
 * @Author wangcf
 * @Date 2023/2/18 15:35
 * @Version 1.0
 */
@WebServlet("/telcheck.action")
public class TelCheckServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentService studentService = new StudentServiceImpl();
        TeacherService teacherService = new TeacherServiceImpl();
        JuryService juryService = new JuryServiceImpl();
        String userStr = req.getParameter("user");
        String tel = req.getParameter("tel");
        Object obj = null;
        //代表没有这个电话允许注册
        boolean flag = false;
        if (userStr.equals("student")) {
            obj = studentService.findByStel(tel);
        } else if (userStr.equals("teacher")) {
            obj = teacherService.findByTel(tel);
        } else if (userStr.equals("jury")) {
            obj = juryService.findByTel(tel);
        }
        if (obj != null) {
            flag = true;
        }
        resp.getWriter().print(flag);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
